//
//  FCCMessageCells.swift
//  chatUIDemo
//
//  Created by admin on 2025/7/10.
//

import UIKit
import SnapKit

// MARK: - 消息Cell基类
class FCCBaseMessageCell: UITableViewCell {
    
    // MARK: - UI组件
    let qfcAvatarImageView = UIImageView()
    let qfcBubbleView = UIView()
    let qfcTimestampLabel = UILabel()
    let qfcStatusIndicator = UIImageView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        qfcSetupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI设置
    private func qfcSetupUI() {
        selectionStyle = .none
        backgroundColor = .clear
        
        // 头像设置
        qfcAvatarImageView.layer.cornerRadius = 20
        qfcAvatarImageView.clipsToBounds = true
        qfcAvatarImageView.backgroundColor = .lightGray
        contentView.addSubview(qfcAvatarImageView)
        
        // 气泡设置
        qfcBubbleView.layer.cornerRadius = 12
        contentView.addSubview(qfcBubbleView)
        
        // 时间标签设置
        qfcTimestampLabel.font = UIFont.systemFont(ofSize: 12)
        qfcTimestampLabel.textColor = .gray
        contentView.addSubview(qfcTimestampLabel)
        
        // 状态指示器设置
        qfcStatusIndicator.contentMode = .scaleAspectFit
        contentView.addSubview(qfcStatusIndicator)
        
        qfcSetupConstraints()
    }
    
    private func qfcSetupConstraints() {
        // 头像约束
        qfcAvatarImageView.snp.makeConstraints { make in
            make.width.height.equalTo(40)
            make.top.equalTo(contentView).offset(8)
        }
        
        // 气泡约束
        qfcBubbleView.snp.makeConstraints { make in
            make.top.equalTo(contentView).offset(8)
            make.bottom.lessThanOrEqualTo(contentView).offset(-8)
            make.width.lessThanOrEqualTo(250)
        }
        
        // 时间标签约束
        qfcTimestampLabel.snp.makeConstraints { make in
            make.top.equalTo(qfcBubbleView.snp.bottom).offset(4)
            make.bottom.equalTo(contentView).offset(-8)
        }
        
        // 状态指示器约束
        qfcStatusIndicator.snp.makeConstraints { make in
            make.width.height.equalTo(16)
            make.bottom.equalTo(qfcBubbleView)
        }
    }
    
    // MARK: - 配置消息
    func qfcConfigureMessage(_ qfcMessage: FCCMessage) {
        // 设置头像
        if qfcMessage.qfcSenderAvatarUrl.isEmpty {
            qfcAvatarImageView.image = UIImage(systemName: "person.circle.fill")
        } else {
            // 这里可以添加网络图片加载逻辑
            qfcAvatarImageView.image = UIImage(systemName: "person.circle.fill")
        }
        
        // 设置时间
        let qfcFormatter = DateFormatter()
        qfcFormatter.dateFormat = "HH:mm"
        qfcTimestampLabel.text = qfcFormatter.string(from: qfcMessage.qfcTimestamp)
        
        // 设置状态
        qfcConfigureStatus(qfcMessage.qfcStatus)
        
        // 设置布局方向
        qfcConfigureLayout(qfcMessage.qfcIsOutgoing)
    }
    
    private func qfcConfigureStatus(_ qfcStatus: FCCMessageStatus) {
        switch qfcStatus {
        case .qfcSending:
            qfcStatusIndicator.image = UIImage(systemName: "clock")
            qfcStatusIndicator.tintColor = .gray
        case .qfcSent:
            qfcStatusIndicator.image = UIImage(systemName: "checkmark")
            qfcStatusIndicator.tintColor = .systemBlue
        case .qfcFailed:
            qfcStatusIndicator.image = UIImage(systemName: "exclamationmark.circle")
            qfcStatusIndicator.tintColor = .red
        }
    }
    
    private func qfcConfigureLayout(_ qfcIsOutgoing: Bool) {
        // 使用SnapKit重新设置约束
        qfcAvatarImageView.snp.remakeConstraints { make in
            make.width.height.equalTo(40)
            make.top.equalTo(contentView).offset(8)
            if qfcIsOutgoing {
                make.trailing.equalTo(contentView).offset(-16)
            } else {
                make.leading.equalTo(contentView).offset(16)
            }
        }
        
        qfcBubbleView.snp.remakeConstraints { make in
            make.top.equalTo(contentView).offset(8)
            make.bottom.lessThanOrEqualTo(contentView).offset(-8)
            make.width.lessThanOrEqualTo(250)
            if qfcIsOutgoing {
                make.trailing.equalTo(qfcAvatarImageView.snp.leading).offset(-8)
            } else {
                make.leading.equalTo(qfcAvatarImageView.snp.trailing).offset(8)
            }
        }
        
        qfcTimestampLabel.snp.remakeConstraints { make in
            make.top.equalTo(qfcBubbleView.snp.bottom).offset(4)
            make.bottom.equalTo(contentView).offset(-8)
            if qfcIsOutgoing {
                make.trailing.equalTo(qfcBubbleView)
            } else {
                make.leading.equalTo(qfcBubbleView)
            }
        }
        
        qfcStatusIndicator.snp.remakeConstraints { make in
            make.width.height.equalTo(16)
            make.bottom.equalTo(qfcBubbleView)
            if qfcIsOutgoing {
                make.trailing.equalTo(qfcBubbleView.snp.leading).offset(-4)
            }
        }
        
        qfcBubbleView.backgroundColor = qfcIsOutgoing ? UIColor.systemBlue : UIColor.systemGray5
        qfcStatusIndicator.isHidden = !qfcIsOutgoing
    }
}

// MARK: - 文本消息Cell
class FCCTextMessageCell: FCCBaseMessageCell {
    
    let qfcContentLabel = UILabel()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        qfcSetupTextUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func qfcSetupTextUI() {
        qfcContentLabel.numberOfLines = 0
        qfcContentLabel.font = UIFont.systemFont(ofSize: 16)
        qfcBubbleView.addSubview(qfcContentLabel)
        
        qfcContentLabel.snp.makeConstraints { make in
            make.top.equalTo(qfcBubbleView).offset(8)
            make.leading.equalTo(qfcBubbleView).offset(12)
            make.trailing.equalTo(qfcBubbleView).offset(-12)
            make.bottom.equalTo(qfcBubbleView).offset(-8)
        }
    }
    
    func qfcConfigureTextMessage(_ qfcMessage: FCCTextMessage) {
        super.qfcConfigureMessage(qfcMessage)
        qfcContentLabel.text = qfcMessage.qfcContent
        qfcContentLabel.textColor = qfcMessage.qfcIsOutgoing ? .white : .black
    }
}

// MARK: - 图片消息Cell
class FCCImageMessageCell: FCCBaseMessageCell {
    
    let qfcImageContentView = UIImageView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        qfcSetupImageUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func qfcSetupImageUI() {
        qfcImageContentView.contentMode = .scaleAspectFill
        qfcImageContentView.clipsToBounds = true
        qfcImageContentView.layer.cornerRadius = 8
        qfcBubbleView.addSubview(qfcImageContentView)
        
        qfcImageContentView.snp.makeConstraints { make in
            make.top.equalTo(qfcBubbleView).offset(4)
            make.leading.equalTo(qfcBubbleView).offset(4)
            make.trailing.equalTo(qfcBubbleView).offset(-4)
            make.bottom.equalTo(qfcBubbleView).offset(-4)
            make.width.equalTo(150)
            make.height.equalTo(150)
        }
    }
    
    func qfcConfigureImageMessage(_ qfcMessage: FCCImageMessage) {
        super.qfcConfigureMessage(qfcMessage)
        
        if let qfcImage = qfcMessage.qfcImage {
            qfcImageContentView.image = qfcImage
        } else {
            qfcImageContentView.image = UIImage(systemName: "photo")
        }
    }
}

// MARK: - 语音消息Cell
class FCCAudioMessageCell: FCCBaseMessageCell {
    
    let qfcPlayButton = UIButton()
    let qfcDurationLabel = UILabel()
    let qfcWaveformView = UIView()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        qfcSetupAudioUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func qfcSetupAudioUI() {
        // 播放按钮
        qfcPlayButton.setImage(UIImage(systemName: "play.fill"), for: .normal)
        qfcPlayButton.tintColor = .darkGray
        qfcBubbleView.addSubview(qfcPlayButton)
        
        // 时长标签
        qfcDurationLabel.font = UIFont.systemFont(ofSize: 12)
        qfcDurationLabel.textColor = .darkGray
        qfcBubbleView.addSubview(qfcDurationLabel)
        
        // 波形视图
        qfcWaveformView.backgroundColor = .lightGray
        qfcWaveformView.layer.cornerRadius = 2
        qfcBubbleView.addSubview(qfcWaveformView)
        
        qfcPlayButton.snp.makeConstraints { make in
            make.leading.equalTo(qfcBubbleView).offset(8)
            make.centerY.equalTo(qfcBubbleView)
            make.width.height.equalTo(24)
        }
        
        qfcDurationLabel.snp.makeConstraints { make in
            make.leading.equalTo(qfcPlayButton.snp.trailing).offset(8)
            make.centerY.equalTo(qfcBubbleView)
        }
        
        qfcWaveformView.snp.makeConstraints { make in
            make.leading.equalTo(qfcDurationLabel.snp.trailing).offset(8)
            make.trailing.equalTo(qfcBubbleView).offset(-8)
            make.centerY.equalTo(qfcBubbleView)
            make.height.equalTo(20)
            make.width.equalTo(80)
        }
        
        qfcBubbleView.snp.makeConstraints { make in
            make.height.equalTo(50)
        }
    }
    
    func qfcConfigureAudioMessage(_ qfcMessage: FCCAudioMessage) {
        super.qfcConfigureMessage(qfcMessage)
        
        qfcDurationLabel.text = "\(qfcMessage.qfcDuration)\""
        let qfcPlayImage = qfcMessage.qfcIsPlaying ? "pause.fill" : "play.fill"
        qfcPlayButton.setImage(UIImage(systemName: qfcPlayImage), for: .normal)
        
        qfcDrawWaveform()
    }
    
    private func qfcDrawWaveform() {
        qfcWaveformView.subviews.forEach { $0.removeFromSuperview() }
        
        let qfcBarCount = 8
        let qfcBarWidth: CGFloat = 3
        let qfcBarSpacing: CGFloat = 2
        
        for qfcIndex in 0..<qfcBarCount {
            let qfcBarView = UIView()
            qfcBarView.backgroundColor = .white
            qfcBarView.layer.cornerRadius = qfcBarWidth / 2
            qfcWaveformView.addSubview(qfcBarView)
            
            qfcBarView.translatesAutoresizingMaskIntoConstraints = false
            let qfcHeight = CGFloat.random(in: 8...20)
            
            NSLayoutConstraint.activate([
                qfcBarView.leadingAnchor.constraint(equalTo: qfcWaveformView.leadingAnchor, constant: CGFloat(qfcIndex) * (qfcBarWidth + qfcBarSpacing)),
                qfcBarView.centerYAnchor.constraint(equalTo: qfcWaveformView.centerYAnchor),
                qfcBarView.widthAnchor.constraint(equalToConstant: qfcBarWidth),
                qfcBarView.heightAnchor.constraint(equalToConstant: qfcHeight)
            ])
        }
    }
}